/**
 * ServiceRequestServiceImpl.java
 * Created at 2017-11-22
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.message.model.SysMessage;
import com.broadtext.message.service.IMessageService;
import com.broadtext.rms.system.dao.IServiceRequestDao;
import com.broadtext.rms.system.model.TmServiceRequest;
import com.broadtext.rms.system.model.TmWoHistory;
import com.broadtext.rms.system.model.TmWorkOrder;
import com.broadtext.rms.system.service.IServiceRequestService;

/**
 * <p>ClassName: ServiceRequestServiceImpl</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-11-22</p>
 */
@Service("serviceRequestService")
public class ServiceRequestServiceImpl extends EntityServiceImpl<TmServiceRequest> implements IServiceRequestService{
    
    
    @Autowired
    @Qualifier("messageService")
    IMessageService messageService;
    
    /**
     * 注入Dao
     */
    @Autowired
    @Qualifier("serviceRequestDao")
    private IServiceRequestDao serviceRequestDao;
    
    
    
    @Autowired
    @Qualifier("serviceRequestDao")
    public void setEntityDao(EntityDao<TmServiceRequest> entityDao) {
        super.entityDao = entityDao;
    }

    /** (non-Javadoc)
     * <p>Title: getPlanById</p>
     * <p>Description: </p>
     * @param pkId
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IServiceRequestService#getPlanById(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    @Override
    public TmServiceRequest getReqById(String pkId) throws ServiceException {
        List<TmServiceRequest> list = new ArrayList<TmServiceRequest>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.TmServiceRequest.getReqById", pkId);
            if(list!=null&&list.size()>0){
                return list.get(0);
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return null;
    }

    /** (non-Javadoc)
     * <p>Title: deleteServiceRequest</p>
     * <p>Description: </p>
     * @param idArray
     * @param updateUser
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IServiceRequestService#deleteServiceRequest(java.lang.String[], java.lang.String)
     */
    @Override
    public void deleteServiceRequest(String[] idArray, String updateUser) throws ServiceException {
      //循环删除 
        try {
            for (String id : idArray) {
                TmServiceRequest t = new TmServiceRequest();
                t.setPkId(id);
                t.setUpdateDate(new Date());
                t.setUpdateUser(updateUser);
                t.setDelFlag("1");
                //删除服务请求
                this.entityDao.updateRecord("com.broadtext.rms.system.model.TmServiceRequest.updateRecord", t);
                //删除工单记录
                TmWorkOrder wo = new TmWorkOrder();
                wo.setRefId(id);
                wo.setUpdateUser(updateUser);
                wo.setUpdateDate(new Date());
                wo.setDelFlag("1");
                this.entityDao.updateRecord("com.broadtext.rms.system.model.TmWorkOrder.deleteWObyRefId", wo);
                //删除工单历史
                this.entityDao.updateRecord("com.broadtext.rms.system.model.TmWoHistory.deleteWOHistoryByRefId", id);
                
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /** (non-Javadoc)
     * <p>Title: saveServiceReq</p>
     * <p>Description: </p>
     * @param t
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IServiceRequestService#saveServiceReq(com.broadtext.rms.system.model.TmServiceRequest)
     */
    @SuppressWarnings("unchecked")
    @Override
    public void saveServiceReq(TmServiceRequest t) throws ServiceException {
        try {
            //保存服务计划
            this.entityDao.insertRecord("com.broadtext.rms.system.model.TmServiceRequest.insertRecord", t);
            //创建服务计划工单
            TmWorkOrder wo = new TmWorkOrder();
            wo.setPkId(UUIDUtils.getUUID());
            wo.setRefId(t.getPkId());
            //"1" 请求工单，"2" 计划工单
            wo.setWoType("1");
            Date now = new Date();
            wo.setCreateUser(t.getCreateUser());
            wo.setCreateDate(now);
            wo.setUpdateUser(t.getCreateUser());
            wo.setUpdateDate(now);
            wo.setDelFlag("0");
            this.entityDao.insertRecord("com.broadtext.rms.system.model.TmWorkOrder.insertRecord", wo);
            //保存工单历史记录--创建工单
            TmWoHistory woHistory = new TmWoHistory();
            woHistory.setPkId(UUIDUtils.getUUID());
            woHistory.setWoId(wo.getPkId());
            woHistory.setOperatorName(t.getUpdateUser());
            woHistory.setOperator(t.getSubmitterName());
            woHistory.setOperatingDate(t.getCreateDate());
            woHistory.setWoStatus("0");
            woHistory.setDelFlag("0");
            this.entityDao.insertRecord("com.broadtext.rms.system.model.TmWoHistory.insertRecord", woHistory);
            
            //给pack厂商推送消息
            //查询所有pack厂用户
            List<String> packFactoryUserList = new ArrayList<String>();
            packFactoryUserList = this.entityDao.findByProperty("com.broadtext.message.model.SysMessage.getAllPackFactoryUser", null);
            if(packFactoryUserList!=null && packFactoryUserList.size()>0 && packFactoryUserList.get(0)!=null) {
                for (String receiver : packFactoryUserList) {
                    SysMessage message = new SysMessage();
                    message.setSender("system");
                    message.setReceiver(receiver);
                    message.setDelFlag("0");
                    message.setMessageTitle("您收到一条新的服务申请，请注意查看");
                    //拼装消息内容
                    StringBuffer content = new StringBuffer("您收到一条新的服务申请“");
                    content.append(t.getRequestContent());
                    content.append("”，您可以在“运营售后管理--服务工单”查看详情。");
                    
                    message.setMessageContent(content.toString());
                    message.setMessageStatus("0");//未读
                    message.setCreateUser(t.getCreateUser());
                    message.setPushDate(now);
                    message.setCreateDate(now);
                    message.setMessageType("2");//服务动态
                    this.messageService.insertMessage(message);
                }
            }
            
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    /** (non-Javadoc)
     * <p>Title: checkRepeatCode</p>
     * <p>Description: </p>
     * @param alertCode
     * @param pkId
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IServiceRequestService#checkRepeatReqCode(java.lang.String, java.lang.String)
     */
    @Override
    public Boolean checkRepeatReqCode(String requestCode, String pkId) throws ServiceException {
        Boolean flag = true;
        Map<String, Object> queryMap = new HashMap<String, Object>();
        if(StringUtils.isNotBlank(requestCode)){
            queryMap.put("requestCode", requestCode);
        }
        if(StringUtils.isNotBlank(pkId)){
            queryMap.put("pkId", pkId);
        }
        long count = 0 ;
        try {
            count = this.serviceRequestDao.checkRepeatReqCode(queryMap);
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        if(count!=0){
            flag = false;
        }
        return flag;
    }
    
    
    

}
